package cn.saltybe.analysis.crawler.wbtc.citiesList;

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
@PipelineName("wbtcCityPipeline")
public class WbtcCityPipeline implements Pipeline<WbtcProvinceList> {
    private static String newHouseEnable;

    @Value("${crawler.wbtc.new-house.enable}")
    public void setNewHouseEnable(String newHouseEnable) {
        this.newHouseEnable = newHouseEnable;
    }

    private static String secondHandHouseEnable;

    @Value("${crawler.wbtc.second-hand-house.enable}")
    public void setSecondHandHouseEnable(String secondHandHouseEnable) {
        this.secondHandHouseEnable = secondHandHouseEnable;
    }

    private static String wholeRentingHouseEnable;

    @Value("${crawler.wbtc.whole-renting-house.enable}")
    public void setWholeRentingHouseEnable(String wholeRentingHouseEnable) {
        WbtcCityPipeline.wholeRentingHouseEnable = wholeRentingHouseEnable;
    }

    private static String shareRentingHouseEnable;

    @Value("${crawler.wbtc.share-renting-house.enable}")
    public void setShareRentingHouseEnable(String shareRentingHouseEnable) {
        this.shareRentingHouseEnable = shareRentingHouseEnable;
    }

    private static String provinceListOrder;

    @Value("${crawler.wbtc.province.order}")
    public void setProvinceListOrder(String provinceListOrder) {
        this.provinceListOrder = provinceListOrder;
    }

    private static String cityListOrder;

    @Value("${crawler.wbtc.city.order}")
    public void setCityListOrder(String cityListOrder) {
        this.cityListOrder = cityListOrder;
    }

    @Override
    public void process(WbtcProvinceList wbtcProvinceList) {
        // 在这里进行抓取到信息的逻辑处理
        int cityCount = wbtcProvinceList.getProvinces().stream().mapToInt(p -> p.getCities().size()).sum();
        System.out.println("已扫描到 " + wbtcProvinceList.getProvinces().size() + " 个省份，共 " + cityCount + " 个城市。");
        int subRequestCount = 0;
        List<WbtcProvince> provinceList = wbtcProvinceList.getProvinces();
        if (provinceListOrder != null && provinceListOrder.toLowerCase().equals("reverse"))
            Collections.reverse(provinceList);
        else if (provinceListOrder != null && provinceListOrder.toLowerCase().equals("shuffle"))
            Collections.shuffle(provinceList);
        for (WbtcProvince p : provinceList) {
            List<WbtcCity> cityList = p.getCities();
            if (cityListOrder != null && cityListOrder.toLowerCase().equals("reverse"))
                Collections.reverse(cityList);
            else if (cityListOrder != null && cityListOrder.toLowerCase().equals("shuffle"))
                Collections.shuffle(cityList);
            for (WbtcCity c : p.getCities()) {
                if (p.getName().equals("其他") || p.getName().equals("海外")) continue;

                // 新房爬取
                if (newHouseEnable != null && newHouseEnable.toLowerCase().equals("true")) {
                    HttpRequest currentRequest = wbtcProvinceList.getRequest();
                    HttpRequest subRequest = currentRequest.subRequest(c.getUrl() + "xinfang");
                    subRequest.addParameter("province", p.getName());
                    subRequest.addParameter("city", c.getName());
                    SchedulerContext.into(subRequest);
                    subRequestCount++;
                }

                // 二手房爬取
                if (secondHandHouseEnable != null && secondHandHouseEnable.toLowerCase().equals("true")) {
                    HttpRequest currentRequest = wbtcProvinceList.getRequest();
                    HttpRequest subRequest = currentRequest.subRequest(c.getUrl() + "ershoufang");
                    subRequest.addParameter("province", p.getName());
                    subRequest.addParameter("city", c.getName());
                    SchedulerContext.into(subRequest);
                    subRequestCount++;
                }

                // 合租房爬取
                if (shareRentingHouseEnable != null && shareRentingHouseEnable.toLowerCase().equals("true")) {
                    HttpRequest currentRequest = wbtcProvinceList.getRequest();
                    HttpRequest subRequest = currentRequest.subRequest(c.getUrl() + "hezu");
                    subRequest.addParameter("province", p.getName());
                    subRequest.addParameter("city", c.getName());
                    SchedulerContext.into(subRequest);
                    subRequestCount++;
                }

                // 整租房爬取
                if (wholeRentingHouseEnable != null && wholeRentingHouseEnable.toLowerCase().equals("true")) {
                    HttpRequest currentRequest = wbtcProvinceList.getRequest();
                    HttpRequest subRequest = currentRequest.subRequest(c.getUrl() + "zufang");
                    subRequest.addParameter("province", p.getName());
                    subRequest.addParameter("city", c.getName());
                    SchedulerContext.into(subRequest);
                    subRequestCount++;
                }
            }
        }
        System.out.println("已将 " + subRequestCount + " 个请求加入队列。");
    }
}
